package br.edu.ifrn.sgpapp.adapter;

import android.app.Activity;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import br.edu.ifrn.sgpapp.R;

public class MenuPrincipalListAdapter extends BaseAdapter {

	private String[] opcoesMenuPrincipal;
	
	private TypedArray imagensMenuPrincipal;
	
	private Activity context;
	
	public MenuPrincipalListAdapter(Activity context) {
		super();
		this.context = context;
		opcoesMenuPrincipal = context.getResources().getStringArray(R.array.itens_menu_principal);
		imagensMenuPrincipal = context.getResources().obtainTypedArray(R.array.imagens_menu_principal);
	}
	
	@Override
	public int getCount() {
		return opcoesMenuPrincipal.length;
	}

	@Override
	public Object getItem(int position) {
		return opcoesMenuPrincipal[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	private class ViewHolder {
		TextView txtItem;
		ImageView imgItem;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		LayoutInflater inflater = context.getLayoutInflater();
		
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.menu_principal_item, null);
			holder = new ViewHolder();
			holder.txtItem = (TextView) convertView.findViewById(R.id.itemTxt);
			holder.imgItem = (ImageView) convertView.findViewById(R.id.itemImg);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.txtItem.setText(opcoesMenuPrincipal[position]);
		holder.imgItem.setImageDrawable(imagensMenuPrincipal.getDrawable(position));

		return convertView;
	}

}
